#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    LCD_DCXLow();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    //LCD_DCXHigh();
    //HAL_Delay(5);
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    LCD_DCXHigh();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    //HAL_Delay(1);
}
void wr_cmd_parall1_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_dat_parall1_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_cmd_parall2_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
void wr_dat_parall2_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
//**********************************read function begin********************************************
void pb_out(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void pb_in(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t i801_read_byte(void)
{
    uint8_t v;

    LCD_DCXHigh();
    LCD_RDLow();
    v = GPIOB->IDR;
    LCD_RDHigh();

    return v;
}
void rd_parall1_8(uint8_t c, uint8_t *d, uint32_t l)
{
    uint8_t buf[128];

    wr_cmd_parall1_8(c);
    pb_in();
    for (int i = 0; i < l; i++)
    {
        buf[i] = i801_read_byte();
    }
    LCD_CSHigh();
    pb_out();
    memcpy(d, buf, l);
}
//**********************************write display begin********************************************
void wr_displ_parall1_8(uint8_t color)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = color;
    LCD_WRHigh();
}
void wr_displ_parall2_18(uint16_t color1, uint16_t color2)
{
    LCD_DCXHigh();
    LCD_WRLow();
    if (color1 & 0x02)
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_RESET);
    if (color1 & 0x01)
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_RESET);
    GPIOB->ODR = color2;
    LCD_WRHigh();
}
void C096LAB_Init(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);

    HAL_Delay(120); // HAL_Delay 120ms

    wr_cmd_4spi_8bcs(0x11); // Sleep out

    HAL_Delay(120); // HAL_Delay 120ms

    wr_cmd_4spi_8bcs(0xB1);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);

    wr_cmd_4spi_8bcs(0xB2);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);

    wr_cmd_4spi_8bcs(0xB3);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);

    wr_cmd_4spi_8bcs(0xB4); // Dot inversion
    wr_dat_4spi_8bcs(0x03);

    wr_cmd_4spi_8bcs(0xC0);
    wr_dat_4spi_8bcs(0x44);
    wr_dat_4spi_8bcs(0x04);
    wr_dat_4spi_8bcs(0x04);

    wr_cmd_4spi_8bcs(0xC1);
    wr_dat_4spi_8bcs(0xC0);

    wr_cmd_4spi_8bcs(0xC2);
    wr_dat_4spi_8bcs(0x0D);
    wr_dat_4spi_8bcs(0x00);

    wr_cmd_4spi_8bcs(0xC3);
    wr_dat_4spi_8bcs(0x8D);
    wr_dat_4spi_8bcs(0x6A);

    wr_cmd_4spi_8bcs(0xC4);
    wr_dat_4spi_8bcs(0x8D);
    wr_dat_4spi_8bcs(0xEE);

    wr_cmd_4spi_8bcs(0xC5);
    wr_dat_4spi_8bcs(0x14);

    wr_cmd_4spi_8bcs(0xE0);
    wr_dat_4spi_8bcs(0x06);
    wr_dat_4spi_8bcs(0x1C);
    wr_dat_4spi_8bcs(0x16);
    wr_dat_4spi_8bcs(0x16);
    wr_dat_4spi_8bcs(0x36);
    wr_dat_4spi_8bcs(0x2F);
    wr_dat_4spi_8bcs(0x28);
    wr_dat_4spi_8bcs(0x2A);
    wr_dat_4spi_8bcs(0x27);
    wr_dat_4spi_8bcs(0x24);
    wr_dat_4spi_8bcs(0x2C);
    wr_dat_4spi_8bcs(0x35);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x03);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x10);

    wr_cmd_4spi_8bcs(0xE1);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x1A);
    wr_dat_4spi_8bcs(0x0F);
    wr_dat_4spi_8bcs(0x0E);
    wr_dat_4spi_8bcs(0x2E);
    wr_dat_4spi_8bcs(0x2A);
    wr_dat_4spi_8bcs(0x24);
    wr_dat_4spi_8bcs(0x29);
    wr_dat_4spi_8bcs(0x29);
    wr_dat_4spi_8bcs(0x27);
    wr_dat_4spi_8bcs(0x2F);
    wr_dat_4spi_8bcs(0x3C);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x02);
    wr_dat_4spi_8bcs(0x02);
    wr_dat_4spi_8bcs(0x10);

    wr_cmd_4spi_8bcs(0x35);
    wr_dat_4spi_8bcs(0x00);

    wr_cmd_4spi_8bcs(0x3A); // 65k mode
    wr_dat_4spi_8bcs(0x66);

    wr_cmd_4spi_8bcs(0x36);
    wr_dat_4spi_8bcs(0xC0);

    wr_cmd_4spi_8bcs(0x29); // Display on

    wr_cmd_4spi_8bcs(0x2C);
    
    wr_cmd_4spi_8bcs(0x2A);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x00+2);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x7F+2);
    wr_cmd_4spi_8bcs(0x2B);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x00+1);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x9F+1);
}
